package com.vast.metrix.viewer.bustraffic.models;

import java.util.ArrayList;
import java.util.List;

public class BusTrafficCollection {
	private String  busName;
	private boolean isRecording = false;
	private double  defaultMaximumValue = 1000.0;
	private final List<BusTraffic> busTrafficList;
	private final BusTraffic maximumValue;
	
	public BusTrafficCollection(String busName, boolean isRecording) {
		this.busName        = busName;
		this.isRecording    = isRecording;
		this.maximumValue   = new BusTraffic(0, this.defaultMaximumValue, this.defaultMaximumValue, this.defaultMaximumValue, this.defaultMaximumValue);
		this.busTrafficList = new ArrayList<BusTraffic>();
	}
	
	public void add(BusTraffic busTraffic){
		if (busTraffic.activity > this.maximumValue.activity) {
			this.maximumValue.activity = busTraffic.activity;
		}
		if (busTraffic.latency > this.maximumValue.latency) {
			this.maximumValue.latency = busTraffic.latency;
		}
		if (busTraffic.utilization > this.maximumValue.utilization) {
			this.maximumValue.utilization = busTraffic.utilization;
		}
		if (busTraffic.transferByte > this.maximumValue.transferByte) {
			this.maximumValue.transferByte = busTraffic.transferByte;
		}
		if (isRecording()) {
			this.busTrafficList.add(busTraffic);
		}
	}

	public String getBusName() {
		return busName;
	}
	
	public String getBusTrafficAsString(int index, boolean includeTimeStamp) {
		if(index > busTrafficList.size()) {
			return new String("Index"+ index + " is not in the list");
		}
		BusTraffic busTraffic = busTrafficList.get(index);
		
		String trafficText = null;
		if(includeTimeStamp) {
			trafficText = Long.toString(busTraffic.timeStamp);
			trafficText = trafficText + "," + Double.toString(busTraffic.transferByte);
		} else {
			trafficText = Double.toString(busTraffic.transferByte);
		}
		trafficText = trafficText + "," + Double.toString(busTraffic.activity);
		trafficText = trafficText + "," + Double.toString(busTraffic.latency);
		trafficText = trafficText + "," + Double.toString(busTraffic.utilization);
		return trafficText;
	}
	
	public int getSizeOfbusTraffic() {
		return busTrafficList.size();
	}
	
	public synchronized boolean isRecording(){
		return this.isRecording;
	}
	
	public synchronized void setRecording(boolean isRecording) {
		this.isRecording = isRecording;
	}
	
	public void remove(BusTraffic busTraffic){
		this.busTrafficList.remove(busTraffic);
	}
	
	public BusTraffic get(int index) {
		return this.busTrafficList.get(index);
	}
	
	public BusTraffic getMaximumValue() {
		return this.maximumValue;
	}
	
	public void clear(){
		busTrafficList.clear();
	}
}
